{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ebf95e40",
   "metadata": {},
   "outputs": [],
   "source": [
    "from pytube import YouTube\n",
    "from xml.etree.ElementTree import Element, dump\n",
    "import os\n",
    "from moviepy.editor import *\n",
    "from moviepy.video.tools.subtitles import *\n",
    "import random"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a8571596",
   "metadata": {},
   "outputs": [],
   "source": [
    "linklist = ['https://www.youtube.com/watch?v=cTQ3Ko9ZKg8&t=1283s',\n",
    "           'https://www.youtube.com/watch?v=r9PeYPHdpNo',\n",
    "           'https://www.youtube.com/watch?v=R2DU85qLfJQ']\n",
    "\n",
    "for link in linklist:\n",
    "    # 경로, 파일 이름\n",
    "    video_path = './video'\n",
    "    caption_path = './caption'\n",
    "    count = len(os.listdir(video_path)) + 1\n",
    "\n",
    "    # 동영상 다운로드\n",
    "    yt = YouTube(link)\n",
    "    yt.streams.filter(progressive=True, file_extension='mp4').order_by('resolution').desc().first().download(output_path=video_path,filename='{0}.mp4'.format(count))\n",
    "\n",
    "    # 언어 선택\n",
    "    code_list = ['en', 'ko', 'ja', 'fr', 'mn', 'vi', 'th']\n",
    "    for key in yt.captions.keys():\n",
    "        if(key.code in code_list):\n",
    "            lang = key.code\n",
    "            break\n",
    "\n",
    "    # 자막 다운로드\n",
    "    caption = yt.captions.get_by_language_code(lang)\n",
    "    xml = caption.xml_captions\n",
    "    print(xml)\n",
    "    srt = caption.generate_srt_captions() # 수정 필요\n",
    "\n",
    "\n",
    "    filename = os.path.join(caption_path, \"{0}.srt\".format(count))\n",
    "    with open(filename, 'w', encoding='utf8') as file:\n",
    "        # file.write(xml)\n",
    "        file.write(srt)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "935fab5a",
   "metadata": {},
   "outputs": [],
   "source": [
    "count=55\n",
    "num=9287\n",
    "for video in range(3):\n",
    "    i=0\n",
    "    j=1\n",
    "    while True:\n",
    "        try:\n",
    "            randnum1 = random.randrange(0,5)\n",
    "            palette = ['white', 'black', 'blue', 'red', 'green']\n",
    "            randnum2 = random.randrange(8,12)\n",
    "            randnum3 = random.randrange(0,100)\n",
    "            generator = lambda txt: TextClip(txt, font='Georgia-Regular', transparent=True, fontsize=randnum2, color=palette[randnum1])\n",
    "            sub = SubtitlesClip(\"./caption/\"+str(count)+\".srt\", generator).set_pos(['center', randnum3]).subclip(i*5,j*5)\n",
    "            clip = VideoFileClip(\"./video/\"+str(count)+\".mp4\").subclip(i*5,j*5)\n",
    "            clip = clip.resize((128, 128)).set_fps(25)\n",
    "            final = CompositeVideoClip([clip, sub])\n",
    "            final.write_videofile(\"./X/X\"+str(num)+\".mp4\")\n",
    "            clip.write_videofile(\"./Y/Y\"+str(num)+\".mp4\")\n",
    "            i+=1\n",
    "            j+=1\n",
    "            num+=1\n",
    "        except:\n",
    "            print(\"break\")\n",
    "            break\n",
    "    count+=1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c8eae6bf",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "client2",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.13 (default, Mar 29 2022, 02:18:16) \n[GCC 7.5.0]"
  },
  "vscode": {
   "interpreter": {
    "hash": "2d3b842955613c815ed22ab2f4f15c249103ad5f1d2692cedc9e4409afea8be0"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
